/**
 * @param {number[]} nums1
 * @param {number[]} nums2
 * @return {number}
 */
var startTempVariable = new Date();
var findMedianSortedArrays = function(nums1, nums2) {
  startTempVariable= new Date();
  // 合并 concat
  console.log(22)
    // 排序 标准快排
    // 判断是否余数 奇偶
    let num3 = []
    if(nums1 == null || nums1.length == 0 && nums2 == null || nums1.length == 0) return 0;

    if(nums1 == null || nums1.length == 0){
        console.log(jg(nums2))
        return jg(nums2)
    }else if(nums2 == null || nums1.length == 0){
        
         
         return jg(nums1)
    }else{
       
        
       
        num3 = nums1.concat(nums2);
        normalSort(num3,0,num3.length)
        console.log(num3)
        // debugger
        let mid = Math.floor(num3.length / 2)
        return jg(num3)
        
    }
    function normalSort(arr,begin,end){
      console.log(arr,begin,end)
    
      if(arr == null || arr.length ==0)return;
      if(begin >= end) return
      let left = begin;
      let right = end; 
      do{
          do left++;while(left<right && arr[left] < arr[begin])
          do right--;while(left<right && arr[right] > arr[begin])
          if(left<right)exchange(arr,left,right)
          console.log(arr)
      }while(left < right)
      // exchange
     
      let midindex = left == right?right-1:right;
      console.log('中间bg情况',begin,end)
      console.log('中间情况',left,right,midindex)

      exchange(arr,begin,midindex)
      
      normalSort(arr,begin,midindex)
      normalSort(arr,midindex+1,end)

    }
    function exchange(arr,a,b){
      if(arr == null || arr.length == 0)return
      let midd = arr[a]
      arr[a] = arr[b]
      arr[b] = midd
    }
    function jg(arr){
      // if(arr == null || arr.length ==0)return 0;
      if(arr.length == 1)return arr[0]
      let num = arr.length % 2
      let mid = Math.floor(arr.length /2)
      if(num == 0){
          return Number((arr[mid]+arr[mid-1])/2)
      }else{
           return Number(arr[mid])
      }
    }
    
  };
  

  
  var a =[1,3]
  var b = [2]
  console.log(findMedianSortedArrays(a,b))
  console.log(`destructuring: ${new Date() - startTempVariable}ms`);